package com.example.demo.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.AppAiChatMapper;
import com.example.demo.pojo.AppAiChat;
import com.example.demo.service.IAppAiChatService;
import com.example.demo.util.HelloWorld;
import com.example.demo.util.MoonshotAiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@Service
public class AppAiChatServiceImpl extends ServiceImpl<AppAiChatMapper, AppAiChat> implements IAppAiChatService {

    @Autowired
    private AppAiChatMapper appAiChatMapper;

    @Override
    public String chat(Integer userId, String sessionId, String message) {
        AppAiChat appAiChat = new AppAiChat();
        appAiChat.setUser_id(userId);
        appAiChat.setSession_id(sessionId);
        appAiChat.setContent(message);
        appAiChat.setRole(0);
        appAiChat.setType(0);
        appAiChatMapper.insert(appAiChat);
        String chat = HelloWorld.chat(message);
//        String chat = "i am liliLILI";
        appAiChat.setContent(chat);
        appAiChatMapper.insert(appAiChat);
        return chat;
    }

    @Override
    public String analysis(Integer userId, String sessionId, String filePath) {
        AppAiChat appAiChat = new AppAiChat();
        appAiChat.setUser_id(userId);
        appAiChat.setSession_id(sessionId);
        appAiChat.setRole(0);
        appAiChat.setType(1);
        appAiChat.setContent(filePath);
        appAiChatMapper.insert(appAiChat);
        String fileId = MoonshotAiUtils.uploadFile(new File(filePath));
        String id = JSONUtil.parseObj(fileId).getStr("id");
        String fileContent = MoonshotAiUtils.getFileContent(id);
        appAiChat.setRole(1);
        appAiChat.setContent(fileContent);
        appAiChatMapper.insert(appAiChat);
        return fileContent;
    }

    @Override
    public List<AppAiChat> historyList(Integer userId) {
        QueryWrapper<AppAiChat> objectQueryWrapper = new QueryWrapper<>();
        QueryWrapper<AppAiChat> appAiChatQueryWrapper = objectQueryWrapper.eq("userId", userId).groupBy("session_id")
                .orderByDesc("create_time");
        List<AppAiChat> appAiChats = appAiChatMapper.selectList(appAiChatQueryWrapper); //sessionId sessionname create_time
        return appAiChats;
    }

    @Override
    public List<AppAiChat> sessionAll(String sessinId) {
        QueryWrapper<AppAiChat> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("session_id", sessinId).orderByDesc("create_time");
        List<AppAiChat> appAiChats = appAiChatMapper.selectList(objectQueryWrapper);
        return appAiChats;
    }

    @Override
    public void updateName(String sessionId, String name) {
        List<AppAiChat> list = appAiChatMapper.selectList(new QueryWrapper<AppAiChat>().eq("session_id", sessionId));
        for(AppAiChat appAiChat: list) {
            appAiChat.setSession_name(name);
            appAiChatMapper.updateById(appAiChat);
        }
    }

    @Override
    public void deleteSession(String sessionId) {
        QueryWrapper<AppAiChat> appAiChatQueryWrapper = new QueryWrapper<>();
        QueryWrapper<AppAiChat> session_id = appAiChatQueryWrapper.eq("session_id", sessionId);
        appAiChatMapper.delete(session_id);
    }

}

/*

 */


